package com.c.service.admin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;
import com.c.dto.PageDto;
import com.c.model.Data;
import com.c.result.LayResult;

import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import cn.hutool.db.Session;
import cn.hutool.db.sql.Direction;
import cn.hutool.db.sql.Order;

@Service
public class DataService {

	@Autowired
	private DruidDataSource ds;

	public LayResult<Data> getLogList(PageDto pg) {
		Session session = Session.create(ds);
		LayResult<Data> lr = new LayResult<Data>();
		List<Data> rList = new ArrayList<Data>();
		try {
			Page page = new Page(pg.getPage(), pg.getLimit());
			Order order = new Order();
			order.setField("id");
			order.setDirection(Direction.DESC);
			page.setOrder(order);
			PageResult<Entity> result = session.page(Entity.create("data"), page);
			for (int i = 0; i < result.size(); i++) {
				Data data = new Data();
				Entity entity = result.get(i);
				data.setMoney(entity.getStr("money"));
				data.setName(entity.getStr("name"));
				data.setNumber(entity.getStr("number"));
				data.setYear(entity.getStr("year"));
				data.setMd5(entity.getStr("md5"));
				rList.add(data);
			}
			lr.setCount(result.getTotal());
			lr.setData(rList);
			lr.setCode(0);
			lr.setMsg("");
		} catch (SQLException e) {
			e.printStackTrace();
			lr.setMsg(e.getMessage());
		} finally {
			session.close();
		}
		return lr;
	}

}
